<template>
	<view class="an-card-article">
		<!-- 搜索框 -->
		<view class="an-search" v-show="searchShow">
			<uni-search-bar radius="100" clearButton="auto" placeholder="用户名搜索" @confirm="search" @cancel="cancel"/>
		</view>
		
		<uni-grid class="an-grid" :column="3" :show-border="false" :square="false" >
			<uni-grid-item class="an-grid-item" v-for="(product,index) in list" :key="index">
				<view class="image-view" @tap="toInfo(product)">
					<image class="uni-product-image" :src="product.thumbnail"></image>
				</view>
				<view class="an-title">{{product.title}}</view>
			</uni-grid-item>
		</uni-grid>
		<!-- 加载更多组件 -->
		<uni-load-more :loadingType="loadingType"></uni-load-more>
		
	</view>
</template>

<script>
	import uniGrid from '@/components/uni-grid/uni-grid.vue'
	import uniGridItem from '@/components/uni-grid-item/uni-grid-item.vue'
	import uniLoadMore from '@/components/uni-load-more.vue'
	import uniSearchBar from '@/components/uni-search-bar/uni-search-bar.vue'
	import {isEmpty} from '@/common/graceChecker.js'
	export default {
		components: {
			uniGrid,
			uniGridItem,
			uniLoadMore,
			uniSearchBar
		},
		data() {
			return {
				list: [],
				total: 0,
				loadingType: 0, //上拉的状态：0-loading前；1-loading中；2-没有更多了
				postData:{
					page: 1,
					pageSize: 12,
					title:'',
					status: 1,
					columnId: ''	//栏目ID
				},
				searchShow: false,	// 是否显示搜索框
			}
		},
		onLoad(option) {
		   	this.postData.columnId = option.id
		   	this.selectColumnInfo(option.id)
			this.getList()
		},
		onNavigationBarButtonTap(obj){	// 页面元素 右上角按钮触发
			this.searchShow = this.searchShow ? false : true
		},
		methods: {
			toInfo(item){	// 跳转详情页
				let nd = {...item, isVideo: true}
				uni.navigateTo({
					url: "../article/articleInfo?detailDate=" + encodeURIComponent(JSON.stringify(nd))
				})
			},
		    getList(page=1){
				this.loadingType = 1
				this.postData.page = page
				if(page === 1){
					this.list = []
				}
				this.$api.SelectArticleList(this.postData).then(res=>{
					this.loadingType = 0
					this.list.push(...res.list); 
					this.total = res.total
					this.changeLoading()
				})
			},
			selectColumnInfo(id){	// 查询文章栏目详情
				this.$api.selectColumn({id:id}).then(res=>{
					if(!isEmpty(res)){
						uni.setNavigationBarTitle({
							title: res.name
						})
					}
				})
			},
			changeLoading(){	// 更新加载组件状态
				if(this.total === 0 || Math.ceil(this.total/this.postData.pageSize)<=this.postData.page){
					this.loadingType = 2
				}
			},
			search(res) {	// 搜索组件回调
				this.postData.title = res.value
				this.getList(1)
			},
			cancel(){	// 搜索取消 
				this.postData.title = '' // 搜索项清空
				this.getList(1)
			},
		},
		onReachBottom() {	// 到底触发
			if (this.loadingType !== 0) {
				return;
			}
			if(Math.ceil(this.total/this.postData.pageSize)>this.postData.page && this.total != 0 ){
				this.postData.page += 1
				this.getList(this.postData.page)
			}else{
				this.loadingType = 2
			}
		}
	}
</script>

<style lang="scss" scoped>
.an-card-article{
	width: 100%;
	.an-grid{
		padding: 0 8rpx;
		.an-grid-item{
			.an-title{
				color: #333333;
				word-break: break-all;
				display: -webkit-box;
				overflow: hidden;
				line-height: 1.5;
				-o-text-overflow: ellipsis;
				text-overflow: ellipsis;
				-webkit-box-orient: vertical;
				-webkit-line-clamp: 2;
			}
		}
	}
	.an-grid-item, .image-view, .uni-product-image, .an-title{
		width: 228rpx;
		overflow: hidden;
	}
	
	
}
</style>
